using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ContentAddEdit : AdminBaseUserControl
    {
        public event EventHandler SaveContent;
        public event EventHandler SendEditContent;
        public event EventHandler SendAuthorContent;
        public event EventHandler SendApproverContent;
        public event EventHandler SendPulicContent;
        public event EventHandler SendButtonHidden;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void DoBindContent(object sender, EventArgs e)
        {
            try
            {
                hdfClassID.Value = Convert.ToString(ddlClass.SelectedValue);
                lblStatus.Text = "Edit Content ";
                ContentListSummary cls = (ContentListSummary)sender;
                long contentID = Convert.ToInt64(cls.SelectedID);
                IContent content = ContentController.GetContent(contentID);
                ViewState["Content"] = content;
                ucCmsExtPropertiesForm.PropertyList = content.ExtPropertyList;

                if (content.Status == ContentStatus.Publishing)
                {
                    if (SendPulicContent != null)
                    {
                        SendPulicContent(this, new EventArgs());
                    }
                }
                else
                {
                    if (SendAuthorContent != null)
                    {
                        SendAuthorContent(this, new EventArgs());
                    }
                }                

                tbAuthorNews.Text = content.AuthorNews;
                tinyMCEContent.Text = content.Content;
                tbSource.Text = content.Source;
                tinyMCESummary.Text = content.Summary;
                tbtag.Text = content.Tags;
                tbTitle.Text = content.Title;
                ddlLanguage.SelectedValue = content.Lang;
                if (content.IsHot == true?cbHot.Checked = true: cbHot.Checked = false) 
                ucContentNoteList.BindNode(content.NoteList);
                ddlCategory.DataBind();
                ///Tuong changed
                ddlClass.SelectedValue = Convert.ToString(content.ClassID);
                ddlPage.DataBind();

                if (ucCmsExtPropertiesForm.PropertyList.Count > 13)
                    fsExtend.Visible = true;
                else fsExtend.Visible = false;
                try
                {
                    ddlPage.SelectedValue = content.PageID.ToString();
                }
                catch { }
                try
                {
                    ddlCategory.SelectedValue = content.CategoryID.ToString();
                }
                catch { }
                

            }
            catch (Exception ex)
            {
                Logger.Debug("[[Error]]", ex);
            }
        }

        public void DoCopyContent(object sender, EventArgs e)
        {
            ViewState["Content"] = null;
        }

        public void DoNewContent(object sender, EventArgs e)
        {
            ddlClass.DataBind();
            ddlClass.Enabled = true;
            ddlClass.SelectedIndex = 0;
            hdfClassID.Value = Convert.ToString(ddlClass.SelectedValue);
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClass.SelectedValue));
            Session[SessionVariable.Clazz.CurrentID] = ddlClass.SelectedValue;

            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;            
            tbAuthorNews.Text="";
            tinyMCEContent.Text = "";
            tinyMCESummary.Text = "";
            tbSource.Text="";
            tbtag.Text="";
            tbTitle.Text = "";
            tbTitle.Focus();
            cbHot.Checked = false;
            IGlobalist.Cms.Model.ContentNoteList listnode = new IGlobalist.Cms.Model.ContentNoteList();
            ViewState["contentNoteList"] = null;
            ucContentNoteList.BindNode(listnode);
            ddlPage.DataBind();
            ddlCategory.DataBind();
            ViewState["Content"] = null;
            if (ucCmsExtPropertiesForm.PropertyList.Count > 13)
                fsExtend.Visible = true;
            else fsExtend.Visible = false;

            if (SendButtonHidden != null)
            {
                SendButtonHidden(this, new EventArgs());
            }
           
        }

        public void DoHidden(object sender, EventArgs e)
        {
            Visible = false;
        }

        public void DoShow(object sender, EventArgs e)
        {
            Visible = true;
        }

        public void DoSaveContent(object sender, EventArgs e)
        {
            try
            {                
                IContent content = new IGlobalist.Cms.Model.Content();
                if (ViewState["Content"] != null)
                {
                    content = (IContent)ViewState["Content"];
                    content.EditedBy = IGlobalistMembership.GetUser().ID;
                }
                else
                {
                    content.Class = ClassController.GetClass(Convert.ToInt64(ddlClass.SelectedValue));
                    content.ClassID = Convert.ToInt64(ddlClass.SelectedValue);
                    content.ClassTypeID = Convert.ToInt16(CmsObjectType.Content);
                    content.CreatedDate = DateTime.Now;
                    content.Author = IGlobalistMembership.GetUser().ID;
                }
                content.AuthorNews = tbAuthorNews.Text;
                Session[SessionVariable.Clazz.CurrentID] = Convert.ToInt64(ddlClass.SelectedValue);
                ucCmsExtPropertiesForm.PropertyList = content.ExtPropertyList;
                content.Description = "";
                content.Content = tinyMCEContent.Text;
                if (cbHot.Checked == true) content.IsHot = true; else content.IsHot = false;
                content.Name = tbTitle.Text;
                content.Summary = tinyMCESummary.Text;
                content.Source = tbSource.Text;
                content.Tags = tbtag.Text;
                content.Title = tbTitle.Text;
                content.Lang = ddlLanguage.SelectedValue;
                content.PageID = Convert.ToInt64(ddlPage.SelectedValue);
                content.CategoryID = Convert.ToInt64(ddlCategory.SelectedValue);
                content.Status = ContentStatus.Creating;
                content.NoteList = ucContentNoteList.NoteList;                   
                if (ViewState["Content"] != null)
                    ContentController.UpdateContent(content);
                else
                    ContentController.InsertContent(content);
                ViewState["contentNoteList"] = null;
                ViewState["Content"] = null;
                if (SaveContent != null)
                {
                    SaveContent(this, new EventArgs());
                }
            }
            catch(Exception ex)
            {
                Logger.Debug("[[Error]]", ex);
                if (SaveContent != null)
                {
                    SaveContent(this, new EventArgs());

                }
                ddlClass.Enabled = false;
            }
        }

        public void DoSendApproverContent(object sender, EventArgs e)
        {
            if (ViewState["Content"] != null)
            {
                IContent content = (IContent)ViewState["Content"];
                content.Status = ContentStatus.Approving;
                ContentController.UpdateContent(content);

            }
        }

        public void DoSendEditContent(object sender, EventArgs e)
        {
            if (ViewState["Content"] != null)
            {
                IContent content = (IContent)ViewState["Content"];
                content.Status = ContentStatus.Editing;
                ContentController.UpdateContent(content);

            }
        }

        public void DoSendAuthorContent(object sender, EventArgs e)
        {
            if (ViewState["Content"] != null)
            {
                IContent content = (IContent)ViewState["Content"];
                content.Status = ContentStatus.Creating;
                ContentController.UpdateContent(content);

            }
        }

        public void DoPulicContent(object sender, EventArgs e)
        {
            if (ViewState["Content"] != null)
            {
                IContent content = (IContent)ViewState["Content"];
                content.CreatedDate = DateTime.Now;
                content.Status = ContentStatus.Publishing;
                ContentController.UpdateContent(content);

            }
        }

        protected void ddlClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Session[SessionVariable.Clazz.CurrentID] = ddlClass.SelectedValue;
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(ddlClass.SelectedValue));
            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            if (ucCmsExtPropertiesForm.PropertyList.Count > 13) fsExtend.Visible = true;            
            else fsExtend.Visible = false;
        }
    }
}
